<?php

/**
 * Created by PhpStorm.
 * User: gao
 * Date: 2019/7/9
 * Time: 9:35
 */
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Train extends Controller
{
function index(){
        return $this->fetch();
    }
function welcome(){
    return $this->fetch();
}
function trainlist(){
    $res=Db::name("train")->alias("t")->join("trainer tr",'t.trainer_id=tr.trainer_id')->select();
    //dump($res);
    $this->assign('train',$res);
    return $this->fetch();
}
function unrelase($train_id){
    $res=Db::name("train")->find($train_id);
    $data=[
        'train_status'=>0
    ];
    $res=Db::name('train')->where('train_id',$train_id)->update($data);
    if($res){
        $this->redirect('trainlist');
        //方法2：先显示笑脸、添加成功，再跳转列表页
        //$this->success('添加成功','userlist');
    }else {
        $this->error('添加失败', 'trainlist');
    }
}
    function relase($train_id){
        $res=Db::name("train")->find($train_id);
        $data=[
            'train_status'=>1
        ];
        $res=Db::name('train')->where('train_id',$train_id)->update($data);
        if($res){
            $this->redirect('trainlist');
            //方法2：先显示笑脸、添加成功，再跳转列表页
            //$this->success('添加成功','userlist');
        }else {
            $this->error('添加失败', 'trainlist');
        }
    }
function trainedit($train_id){
    $res=Db::name("train")->alias("t")->join("trainer tr",'t.trainer_id=tr.trainer_id')->find($train_id);
    $this->assign('train',$res);
    $trainername=Db::name('trainer')->select();
    $this->assign('trainername',$trainername);
    return $this->fetch();
}
function dotrainedit($train_id){
    $file =request()->file('pic');
    $pic="";
    if($file){
        $info = $file->rule('uniqid')->
        validate(['size'=>3000000,'ext'=>
            'jpg,png,gif,jpeg'])->
        move(ROOT_PATH . 'public' .DS.'static'. DS . 'upload');
        if($info){
            $pic=$info->getFilename();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
    $data = [
        'train_address' => input('train_address'),
        'train_start' => input('train_start'),
        'train_pic' => $pic,
        'train_stop' => input('train_stop'),
        'train_price' => input('train_price'),
        'trainer_id' => input('trainer_id'),
        'train_intro' => input('train_intro'),
    ];
    //存入数据库
    //dump($data);
    $res=Db::name('train')->where('train_id',$train_id)->update($data);
    if($res){
        $this->redirect('trainlist');
        //方法2：先显示笑脸、添加成功，再跳转列表页
        //$this->success('添加成功','userlist');
    }else {
        $this->error('添加失败', 'trainlist');
    }
}
function trainadd(){
    $trainername=Db::name('trainer')->select();
    $this->assign('trainername',$trainername);
    return $this->fetch();
}
function dotrainadd(){
    $file =request()->file('pic');
    $pic="";
    if($file){
        $info = $file->rule('uniqid')->
        validate(['size'=>3000000,'ext'=>
            'jpg,png,gif,jpeg'])->
        move(ROOT_PATH . 'public' .DS.'static'. DS . 'upload');
        if($info){
            $pic=$info->getFilename();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    //判断用户名是否已经存在
    $trainname=input('train_name');
   $num=Db::name("train")->where("train_name",$trainname)->count();
   if($num>0)
        $this->error("该训练已存在");
    //获取表单数据
    $data = ['train_name' => $trainname,
        'train_pic' => $pic,
        'train_address' => input('train_address'),
        'train_start' => input('train_start'),
        'train_stop' => input('train_stop'),
        'train_price' => input('train_price'),
        'trainer_id' => input('trainer_id'),
        'train_intro' => input('train_intro'),
        'maxnum'=>input('maxnum'),
        'joinnum'=>0
        ];
    //存入数据库
    //dump($data);
    $res=Db::name('train')->insert($data);
    if($res){
        $this->redirect('trainlist');
        //方法2：先显示笑脸、添加成功，再跳转列表页
        //$this->success('添加成功','userlist');
    }else {
        $this->error('添加失败', 'trainlist');
    }
}
function deltrain($train_id){
    if(Db::name('train')->delete($train_id)){
        $this->redirect('trainlist');
        //$this->success('删除成功','userlist');
    }else $this->error('删除失败','trainlist');
}
function enlist($train_id){
    //$activity=Db::name('activity')->find($activity_id);
    $enlist=Db::name("train")->alias("t")->join("train_order to",'to.train_id=t.train_id')
        ->join("user u",'to.user_id=u.user_id')->order("train_order_addtime desc")->select($train_id);
    //dump($enlist);exit;
    //活动id，订单表，用户表，
//    $value=$enlist['activity_value'];
//    $ammount=$enlist['order_activity_ammount'];
//    dump($value);exit;
//    $price=$value*$ammount;
    //把变量保存在模版变量中
    //$this->assign('activity',$activity);
//    $this->assign('price',$price);
    $this->assign('enlist',$enlist);
    return $this->fetch();

}
}